﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using FukiERP.Common.Constants;
using FukiERP.Helper;
using FukiERP.Services;
using FukiERP.Services.Helper;
using FukiERP.Services.DBContext;
using DBservice = FukiERP.Services.DBContext;

namespace FukiERP.Product
{
    public partial class Form_ModelBrand : Telerik.WinControls.UI.RadForm
    {
        int _currentID = 0;

        public Form_ModelBrand()
        {
            InitializeComponent();
        }

        private void Form_ModelBrand_Load(object sender, EventArgs e) {
            LoadData();
        }

        private void LoadData() {
            grd_ModelBrand.Rows.Clear();
            FukiERPDataContext context = DataAccess.GetDBContext();
            List<ModelBrand> brands = context.ModelBrands.ToList();
            grd_ModelBrand.DataSource = brands;
        }

        private void tbn_Save_Click(object sender, EventArgs e) {
            ModelBrand mbrand = null;
            FukiERPDataContext context = DataAccess.GetDBContext();
            errorProvider1.Clear();
            bool isValid = ValidateValue.ValidateRequirement(errorProvider1, tbx_BrandName);
            if (!isValid) {
                MessageBox.Show(MessageConstants.GaneralValidateMessage);
                return;
            }
            if (_currentID != 0) {
                mbrand = context.ModelBrands.Where(c => c.ID == _currentID).FirstOrDefault();
            }
            if (mbrand == null) {
                mbrand = new ModelBrand();
                context.ModelBrands.InsertOnSubmit(mbrand);
            }
            mbrand.Name = tbx_BrandName.Text;
            mbrand.Description = tbx_BrandDes.Text;
            context.SubmitChanges();
            RefreshForm();
            LoadData();
        }

        private void btn_Delete_Click(object sender, EventArgs e) {
            if (_currentID != 0 && PresentHelper.ShowConfirmBox(MessageConstants.ConfirmDeleteMessage)) {
                FukiERPDataContext context = DataAccess.GetDBContext();
                ModelBrand mbrand = context.ModelBrands.Where(c => c.ID == _currentID).FirstOrDefault();
                if (mbrand != null) {
                    context.ModelBrands.DeleteOnSubmit(mbrand);
                    context.SubmitChanges();
                }
               
                MessageBox.Show(MessageConstants.DeleteSuccessMessage);
                RefreshForm();
                LoadData();
            }
        }

        private void btn_Create_Click(object sender, EventArgs e) {
            RefreshForm();
        }

        private void RefreshForm() {
            _currentID = 0;
            tbx_BrandName.Text = string.Empty;
            tbx_BrandDes.Text = string.Empty;
        }

        private void grd_ProductBrand_CellDoubleClick(object sender, Telerik.WinControls.UI.GridViewCellEventArgs e) {
            _currentID = int.Parse(grd_ModelBrand.Rows[e.RowIndex].Cells[0].Value.ToString());
            ModelBrand brand = (ModelBrand)grd_ModelBrand.Rows[e.RowIndex].DataBoundItem;
            tbx_BrandName.Text = brand.Name;
            tbx_BrandDes.Text = brand.Description;
        }
    }
}
